﻿layui.use('table', function () {
var table = layui.table;
table.render({
    elem: '#tables'
    , height: 740
    , width: 1680
    , url: 'http://124.133.35.234:8000/UserWorkbench/getSolutions/' //数据接口
    , page: false //开启分页
    , limit: 10
    , parseData: function (res) { //res 即为原始返回的数据
        console.log(res);
        return {
            "code": res.code, //解析接口状态
            "msg": res.title, //解析提示文本
            "count": res.total, //解析数据长度
            "data": res.rows //解析数据列表
        }
    }
    , cols: [[ //表头
        { field: 'id', title: 'Id', width: 80, sort: true, align: 'center', }
        , { field: 'Sid', title: '编码', width: 150, sort: true, align: 'center',}, //sort: true,排序
        {
            field: 'title',
            title: '标题',
            //width: 120,
            align: 'center',
        },
        {
            field: 'area',
            title: '行业',
            //width: 120,
            align: 'center',
        },
        {
            field: 'pic',
            title: '图片名称',
            width: 120,
            align: 'center',
        },
        {
            field: 'upfile',
            title: '文件名称',
            width: 120,
            align: 'center',
        },
        {
            field: 'counter',
            title: '下载次数',
            width: 120,
            align: 'center',
        },
        {
            field: 'unit',
            title: '单位',
            width: 120,
            align: 'center',
        },
        {
            field: 'phone',
            title: '联系电话',
            width: 120,
            align: 'center',
        },
        {
            field: 'email',
            title: '邮箱',
            width: 120,
            align: 'center',

        },
        {
            field: 'createby',
            title: '创建人',
            width: 120,
            align: 'center',

        },
        {
            field: 'createtime',
            title: '创建时间',
            width: 120,
            align: 'center',
            templet: function (d) {
                return changeDateFormat(d.createtime)
            }
        },
        {
            field: 'description',
            title: '描述',
            width: 120,
            align: 'center',

        },
        {
            //fixed: 'right',
            title: '操作',
            width: 178,
            align: 'center',
            toolbar: '#barDemo',
            templet: function (d) { return d.createby },
        },
    ]]
});
    //监听工具条
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            layer.msg('ID：' + data.id + ' 的查看操作');
            $('#look_Sid_bm').val(data.Sid);
            $('#look_title').val(data.title);
            $('#look_area').val(data.area);
            $('#look_pic').val(data.pic);
            $('#look_upfile').val(data.upfile);
            $('#look_counter').val(data.counter);
            $('#look_unit').val(data.unit);
            $('#look_phone').val(data.phone);
            $('#look_email').val(data.email);
            $('#look_createby').val(data.createby);
            $('#look_createtime').val(changeDateFormat(data.createtime));
            $('#look_description').val(data.description);
            layer.open({
                type: 1,
                title: '查看',
                shade: 0.5,
                //skin: 'layui-layer-rim', //加上边框
                area: ['660px', '550px'], //宽高
                shade: 0, //不显示遮罩
                btnAlign: 'r'//按钮排列：居中对齐
                , moveType: 1 //拖拽模式，0或者1
                , content: $("#look")
                , cancel: function () {
                    $("#look").hide();
                }
            });

        } else if (obj.event === 'del') {
            //var data = obj.data;
            //layer.confirm("确定要删除ID：" + data.id+"的数据吗", {
            //    btn: ['确定', '取消']
            //}, function () { //按钮1的操作 确定
            //        $.ajax({
            //            type: "POST",
            //            url: "/UserWorkbench/delete_solution?id=" + data.id,
            //            processData: false,
            //            contentType: false,
            //            success: function (data) {
            //                if (data) {
            //                    layer.msg('删除成功');
            //                   location.reload();

            //                } else {
            //                    layer.msg('删除失败');
            //                   location.reload();

            //                }
            //            }
            //        });
            //        layer.msg('确定');
            //}, function () { //按钮1的操作 取消
            //        layer.msg('已取消删除');
            //});
        } else if (obj.event === 'edit') {
            $('#e_id').val(data.id);
            $('#e_Sid_bm').val(data.Sid);
            $('#e_title').val(data.title);
            $('#e_area').val(data.area);
            layui.form.render("select");//layUI设置
            $('#e_pic').val(data.pic);
            $('#e_upfile').val(data.upfile);
            $('#e_unit').val(data.unit);
            $('#e_phone').val(data.phone);
            $('#e_email').val(data.email);
            $('#e_createby').val(data.createby);
            $('#e_createtime').val(changeDateFormat(data.createtime));
            $('#e_description').val(data.description);
            $('#e_counter').val(data.counter);
            var editindex =layer.open({
                type: 1,
                title: '编辑',
                shade: 0.5,
                //skin: 'layui-layer-rim', //加上边框
                area: ['700px', '600px'], //宽高
                shade: 0, //不显示遮罩
                btnAlign: 'r'//按钮排列：居中对齐
                , moveType: 1 //拖拽模式，0或者1
                , btn: '修改'
                , content: $("#edit")
                , cancel: function () {
                    $("#edit").hide();
                },
                yes: function (index, layero) {
                    console.log(index);
                    var e_id= $('#e_id').val();
                    var e_Sid_bm = $('#e_Sid_bm').val();
                    var e_title = $('#e_title').val();
                    var e_area = $('#e_area').val();
                    var e_pic = $('#e_pic').val();
                    var e_upfile = $('#e_upfile').val();
                    var e_unit = $('#e_unit').val();
                    var e_phone = $('#e_phone').val();
                    var e_email = $('#e_email').val();
                    var e_createby = $('#e_createby').val();
                    var e_createtime = $('#e_createtime').val();
                    var e_description = $('#e_description').val();
                    $.ajax({
                        type: "POST",
                        url: "/UserWorkbench/reviseSolutions?e_id=" + e_id + "&e_Sid_bm=" + e_Sid_bm + "&e_title=" + e_title + "&e_area=" + e_area + "&e_pic=" + e_pic + "&e_upfile=" + e_upfile
                            + "&e_unit=" + e_unit + "&e_phone=" + e_phone + "&e_email=" + e_email + "&e_createby=" + e_createby + "&e_createtime=" + e_createtime + "&e_description=" + e_description,
                        processData: false,
                        contentType: false,//必须false才会自动加上正确的Content-Type
                        success: function (data) {
                            //var dataJson = eval('(' + data + ')');    //转成json格式
                            if (data == "true") {
                                layer.msg('修改成功');
                                $("#edit").hide();
                                layer.close(editindex); //关闭编辑窗口
                                location.reload();
                            } else {
                                layer.msg('修改失败');
                                 //vscode.postMessage({
                                 //    command: 'close',
                                 //});
                                $("#edit").hide();
                                layer.close(editindex);  //关闭编辑窗口
                                location.reload();
                            }
                        }
                    });
                }
            });

        }
    });
    var $ = layui.$, active = {
        Add: function () { //获取选中数据
            //页面层
            $("#Sid_bm").val('S'+getTime());  //设置编码
            layer.open({
                type: 1,
                title: '新增',
                //shade: 0.1, //边框
                //skin: 'layui-layer-rim', //加上边框
                area: ['500px', '700px'], //宽高
                shade: 0, //不显示遮罩
                 btnAlign: 'r'//按钮排列：居中对齐
                , moveType: 1 //拖拽模式，0或者1
                ,content: $("#test")
                , btn: '确定'
                , cancel: function () {
                //layer.msg('关闭');
                $("#test").hide();
               },
                yes: function (index, layero) {
                    var bm = $('#Sid_bm').val();
                    var file_upfile = $('#upfile')[0].files;
                    var formData = new FormData();
                    formData.append("file", file_upfile[0]);
                    formData.append('bm', bm);
                    $.ajax({
                        type: "POST",
                        url: '/UserWorkbench/UploadFile_solution',
                        data: formData,
                        processData: false,
                        contentType: false,//必须false才会自动加上正确的Content-Type
                        success: function (data) {
                            if (data.msg = "文件上传完成") {
                                //alert("文件上传完成！！");
                                //上传图片
                                var bm = $('#Sid_bm').val();
                                var file_pic = $('#pic')[0].files;
                                var formData = new FormData();
                                formData.append("file", file_pic[0]);
                                formData.append('bm', bm);
                                $.ajax({
                                    type: "POST",
                                    url: '/UserWorkbench/UploadFile_solution',
                                    data: formData,
                                    processData: false,
                                    contentType: false,//必须false才会自动加上正确的Content-Type
                                    success: function (data) {
                                        if (data.msg = "文件上传完成") {
                                            //alert("图片上传完成！！");
                                            var bm = $('#Sid_bm').val();
                                            var title = $('#title').val();
                                            var area = $('#area').val();
                                            //var pic = $('#pic').val();
                                            //var upfile = $('#upfile').val();
                                            var unit = $('#unit').val();
                                            var phone = $('#phone').val();
                                            var email = $('#email').val();
                                            var createby = $('#createby').val();
                                            var createtime = $('#createtime').val();
                                            var description = $('#description').val();
                                            $.ajax({
                                                type: "POST",
                                                url: "/UserWorkbench/increaseSolutions?Sid_bm=" + bm + "&title=" + title + "&area=" + area + "&pic=" + file_pic[0].name + "&upfile=" + file_upfile[0].name
                                                    + "&unit=" + unit + "&phone=" + phone + "&email=" + email + "&createby=" + createby + "&createtime=" + createtime + "&description=" + description,
                                                processData: false,
                                                contentType: false,//必须false才会自动加上正确的Content-Type
                                                success: function (data) {
                                                    //var dataJson = eval('(' + data + ')');    //转成json格式
                                                    if (data == "true") {
                                                        layer.msg('新增成功');
                                                        $("#test").hide();
                                                        location.reload();
                                                    } else {
                                                        layer.msg('新增失败');
                                                        location.reload();
                                                    }
                                                }
                                            });

                                        } else {
                                            layer.msg('图片上传失败');
                                        }
                                    }

                                });

                            } else {
                                layer.msg('文件上传失败');
                            }
                        }

                    });

                    layer.close(index); //关闭弹出框

                }
            });
        }
    };
    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.reload('tableReload', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        //, where: {//这里传参  向后台
        //    name: name
        //    //可传多个参数到后台...  ，分隔
        //}
        , url: '/UserWorkbench/getSolutions/' //后台做模糊搜索接口路径
        , method: 'post'
    });

});


//转换日期格式
function changeDateFormat(cellval) {
    if (cellval != null) {
        var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }
}
//获取当前时间 （编码格式：年月日小时分钟秒）
function getTime() {
    var myDate = new Date();
    var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var myToday = myDate.getDate(); //获取当前日(1-31)
    var myHour = myDate.getHours(); //获取当前小时数(0-23)
    var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
    var mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
    var nowTime;
    nowTime = myYear  + fillZero(myMonth) + fillZero(myToday) +  fillZero(myHour)  +
        fillZero(myMinute) + fillZero(mySecond);
    return nowTime;
};
function fillZero(str) {
    var realNum;
    if (str < 10) {
        realNum = '0' + str;
    } else {
        realNum = str;
    }
    return realNum;
}
